package com.yuju.account.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.dao.IAccountReportDao;
import com.yuju.account.dao.entity.AccountReport;
import com.yuju.account.model.AccountReportModel;
import com.yuju.account.model.GuestBalanceModel;
import com.yuju.account.service.IAccountReportService;
import com.yuju.common.PageModel;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.util.DateUtil;
import com.yuju.util.ExcelExp;
import com.yuju.util.Utils;

/**
 * 类名：AccountReportServiceImpl  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月20日 下午5:49:55  <br />
 * @version : 1.1 <br />
 */
@Service
public class AccountReportServiceImpl extends AbstractBaseService<AccountReportModel, AccountReport, String> implements IAccountReportService {
	
	@Autowired
	private IAccountReportDao accountReportDao;
	
	@Override
	public IBaseDao<AccountReportModel, AccountReport, String> getDao() {
		return accountReportDao;
	}

	/**
	 * 功能:订单账务报表接口 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月21日 上午11:00:50<br/>
	 * 
	 * @param accountReportModel
	 * @param currentPage
	 * @param rows
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountReportService#listOrderAndCount(com.yuju.account.model.AccountReportModel, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public PageModel listOrderAndCount(AccountReportModel accountReportModel, Integer currentPage, Integer rows) {
		return accountReportDao.listOrderAndCount(accountReportModel, currentPage, rows);
	}

	/**
	 * 功能:会员消费查询接口 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月22日 上午10:35:13<br/>
	 * 
	 * @param accountReportModel
	 * @param currentPage
	 * @param rows
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountReportService#listMemberOrderAndCount(com.yuju.account.model.AccountReportModel, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public PageModel listMemberOrderAndCount(AccountReportModel accountReportModel, Integer currentPage, Integer rows) {
		return accountReportDao.listMemberOrderAndCount(accountReportModel, currentPage, rows);
	}

	@Override
	public List<GuestBalanceModel> guestBalanceReport(AccountReportModel accountReportModel) {
		return accountReportDao.guestBalanceReport(accountReportModel);
	}

	@Override
	public PageModel guestBalanceReportPage(AccountReportModel accountReportModel, Integer currentPage, Integer rows) {
		return accountReportDao.guestBalanceReportPage(accountReportModel, currentPage, rows);
	}

	@Override
	public GuestBalanceModel guestBalanceSumRpt(AccountReportModel accountReportModel) {
		return accountReportDao.guestBalanceSumRpt(accountReportModel);
	}

	@Override
	public byte[] expBalanceRpt(AccountReportModel accountReportModel) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		e.setCell(0, "房号");
		e.setCell(1, "姓名");
		e.setCell(2, "类型");
		e.setCell(3, "抵店");
		e.setCell(4, "房价");
		e.setCell(5, "押付方式");
		e.setCell(6, "押金");
		e.setCell(7, "收款");
		e.setCell(8, "总消费");
		e.setCell(9, "余额");
		e.setCell(10, "客户");
		e.setColumnWidth(0, 4000);
		e.setColumnWidth(1, 4000);
		e.setColumnWidth(2, 4000);
		e.setColumnWidth(3, 6000);
		e.setColumnWidth(4, 4000);
		e.setColumnWidth(5, 4000);
		e.setColumnWidth(6, 4000);
		e.setColumnWidth(7, 4000);
		e.setColumnWidth(8, 4000);
		e.setColumnWidth(9, 4000);
		e.setColumnWidth(10,4000);
		
		List<GuestBalanceModel> list = this.guestBalanceReport(accountReportModel);
		GuestBalanceModel model = this.guestBalanceSumRpt(accountReportModel);
		if(Utils.isNull(model) || Utils.isNull(list) ){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		for (int i = 0; i < list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getRoomNo());
			e.setCell(1, list.get(i).getName());
			e.setCell(2, list.get(i).getType());
			e.setCell(3, DateUtil.format(list.get(i).getCheckinTime()));
			e.setCell(4, list.get(i).getPrice().toString());
			e.setCell(6, list.get(i).getSumYJ().toString());
			e.setCell(7, list.get(i).getSumSS().toString());
			e.setCell(8, list.get(i).getSumYS().toString());
			e.setCell(9, list.get(i).getBalance().toString());
			e.setCell(10, list.get(i).getCustomTypeName());
		}
		e.createRow(list.size()+1);
		e.setCell(0, "合计");
		e.setCell(4, model.getPrice().toString());
		e.setCell(6, model.getSumYJ().toString());
		e.setCell(7, model.getSumSS().toString());
		e.setCell(8, model.getSumYS().toString());
		e.setCell(9, model.getBalance().toString());
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}



}
